package hn.sigit.logic.workflow.transaction.impl;

import javax.faces.convert.Converter;

import hn.sigit.dao.hnd.ladmshadow.PartyDAO;
import hn.sigit.dao.ladm.party.LA_PartyDAO;
import hn.sigit.logic.ladmshadow.Util;
import hn.sigit.model.commons.IParty;
import hn.sigit.model.commons.IRRR;
import hn.sigit.model.hnd.ladmshadow.RRR;
import hn.sigit.model.ladm.external.ExtParty;
import hn.sigit.model.ladm.party.LA_Party;
import hn.sigit.model.ladm.special.Rational;
import hn.sigit.util.ShareValue;

public abstract class ParcelRRR<RT extends Enum<?>> {
	private boolean selected;
	private boolean modified = false;
	private boolean newRRR = false;
	private ExtParty extParty;
	private RT rrrType;
	private ShareValue share;
	protected long presentationId;
	protected IRRR rrr;
	
	private ShareValue backShare;
	
	private Converter rrrTypeConverter;
	
	
	public ParcelRRR(long presentationId, ExtParty extParty, RT rrrType, IRRR rrr, Converter rrrTypeConverter) {
		this(presentationId, extParty, rrrType, new ShareValue(), rrr, rrrTypeConverter);
	}
	public ParcelRRR(long presentationId, ExtParty extParty, RT rrrType, ShareValue share, IRRR rrr, Converter rrrTypeConverter) {
		this.presentationId = presentationId;
		this.extParty = extParty;
		this.rrrType = rrrType;
		this.share = share;
		this.rrr = rrr;
		this.rrrTypeConverter = rrrTypeConverter;
		
		this.backShare = this.share.clone();
	}
	
	public RT getRrrType() {
		return rrrType;
	}
	public void setRrrType(RT rrrType) {
		if (this.rrrType != rrrType) {
			this.rrrType = rrrType;
			setModified(true);
		}
	}
	//This has proven to make it work on Mac OS X servers
	@SuppressWarnings("unchecked")
	public void setRrrType(Object rrrType) {
		setRrrType((RT) rrrType);
	}
	
	public String getTypeString() {
		if (rrrType == null) return "";
		
		return rrrType.name();
	}
	public void setTypeString(String value) {
		System.out.println("tipo: " + value);
	}


	
	public boolean isSelected() {
		return selected;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	public boolean isModified() {
		return modified;
	}
	protected void setModified(boolean modified) {
		this.modified = modified;
	}
	
	public boolean isNewRRR() {
		return newRRR;
	}
	public void setNewRRR(boolean newRRR) {
		this.newRRR = newRRR;
	}
	
	public ExtParty getExtParty() {
		return extParty;
	}
	public void setExtParty(ExtParty extParty) {
		if (this.extParty != extParty) {
			this.extParty = extParty;
			setModified(true);
		}
	}
	
	
	public ShareValue getShare() {
		return share;
	}
	public void setShare(ShareValue share) {
		if (!this.share.equals(share)) {
			this.share = share;
			setModified(true);
		}
	}
	
	public long getPresentationId() {
		return presentationId;
	}
	/*
	public void setPresentationId(long presentationId) {
		this.presentationId = presentationId;
	}*/
	
	public String getOwner() {
		if (extParty != null)
			return extParty.getName();
		return "";
	}

	public String getShareString() {
		return share.toString();
	}
	
	public IRRR getRrr() {
		return rrr;
	}
	
	public Converter getRrrTypeConverter() {
		return rrrTypeConverter;
	}
	
	@SuppressWarnings("unchecked")
	public void cancelChanges() {
		if (rrr == null || !modified) return;
		
		rrrType = (RT) rrr.getType();
		Rational rrrShare = rrr.getShare();
		if (rrrShare != null) {
			share.setNumerator(rrrShare.getNumerator());
			share.setDenominator(rrrShare.getDenominator());
		}
		else {
			share.setNumerator(0);
			share.setDenominator(1);
		}
		
		IParty rrrParty = rrr.getParty();
		if (rrrParty != null && rrrParty.getExtParty() != null)
			extParty = rrrParty.getExtParty();
	}
	
	public void applyChanges() {
		if (rrr == null) return;

		setModified(modified || !share.equals(backShare));
		if (!isModified()) return;
		
		rrr.setType(getRrrType());
		rrr.setShare(share.asLadmRational());
		
		if (rrr.getParty() == null || rrr.getParty().getExtParty() != extParty) {
			LA_Party laParty = LA_PartyDAO.loadPartyByExtParty(extParty);
			IParty party = Util.LA_PartyToParty(laParty, presentationId, false, false);
			PartyDAO.save(party);
			
			rrr.setParty(party);
		}
		
		((RRR) rrr).setModified(true);
	}
}
